package vip.xiaonuo.modular.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import vip.xiaonuo.modular.entity.GpsInfo;
import vip.xiaonuo.modular.entity.Shipment;
import vip.xiaonuo.modular.entity.dto.ShipmentDTO;
import vip.xiaonuo.modular.mapper.ShipmentMapper;
import vip.xiaonuo.modular.service.ShipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 运单信息表 服务实现类
 * </p>
 *
 * @author tuchaoren
 * @since 2021-06-25
 */
@Service
public class ShipmentServiceImpl extends ServiceImpl<ShipmentMapper, Shipment> implements ShipmentService {

    @Autowired
    private ShipmentMapper shipmentMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private final String asyncInsertShipment = "ASYNC:INSERT:WAYBILL:";

    @Override
    @Async(value = "insertExecutor")
    public void batchInsertShipment(List<Shipment> shipments) {
        // 循环新增运单信息


        List<String> orders = new ArrayList<String>();
        shipments.forEach(data -> {
            orders.add(data.getShipmentNo());
        });

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("shipment_no", orders);
        List<Shipment> dataList = shipmentMapper.selectList(queryWrapper);
        java.util.Map<String, Shipment> shipmentMap = new HashMap<String, Shipment>();
        dataList.forEach(data -> {
            shipmentMap.put(data.getShipmentNo(), data);

        });
        shipments.forEach(data -> {
            // 相同运单只存储一条信息
            if (StrUtil.isNotEmpty(data.getShipmentNo())) {
                data.setUpdateTime(new Date());
                Shipment shipmentthis = shipmentMap.get(data.getShipmentNo());
                if(shipmentthis != null){
                    data.setId(shipmentthis.getId());
                    data.setCreateTime(shipmentthis.getCreateTime());
                }else{
                    data.setCreateTime(new Date());
                }
            }
        });
        this.saveOrUpdateBatch(shipments);

    }

    @Override
    public String findBeginDate() {
        return shipmentMapper.findBeginDate();
    }

    /**
     * 新增运单信息
     */
    private boolean insertShipment(Shipment shipment) {
        return super.save(shipment);
    }

    /**
     * 根据运单号判断是否存储该运单信息
     */
    private Shipment queryShipmentByShipmentNo(String shipmentNo) {
        LambdaQueryWrapper<Shipment> lambdaQuery = Wrappers.<Shipment>lambdaQuery()
                .eq(Shipment::getShipmentNo, shipmentNo);
        return shipmentMapper.selectOne(lambdaQuery);
    }
}
